<template>
  <dialog_item :dialogTitle="$t('km_role')" @onBtnDialogClick="onBtnDialogClick" :visible.sync="dialogVisible" dialogClass="dialogHeight" :isShowConfirmBtn="true">
    <div class="km-dialog">
      <!-- <el-divider content-position="left">角色选择</el-divider> -->
      <p class="smallHeader">{{$t('select_roles')}}</p>
      <el-row>
        <el-col :span="24">
        <el-checkbox-group v-model="checkedKMRoles" v-if="staticPath">
          <el-checkbox label="KMNORMALROLEID">{{$t('staff')}}</el-checkbox>
          <el-checkbox label="KMDEPTADMINROLEID">{{$t('dept_know_manager')}}</el-checkbox>
          <el-checkbox label="KMCOMMITTEEADMINROLEID">{{$t('committee_know_manager')}}</el-checkbox>
          <el-checkbox label="KMSUPERADMINROLEID">{{$t('enterprise_know_manager')}}</el-checkbox>
        </el-checkbox-group> 
        <el-checkbox-group v-model="checkedKMRoles" v-else>
          <el-checkbox label="0001">{{$t('staff')}}</el-checkbox>
          <el-checkbox label="0010">{{$t('dept_know_manager')}}</el-checkbox>
          <el-checkbox label="0020">{{$t('committee_know_manager')}}</el-checkbox>
          <el-checkbox label="0100">{{$t('enterprise_know_manager')}}</el-checkbox>
        </el-checkbox-group> 
        </el-col>
      </el-row>
      
      <el-row>
        <el-col :span="24">
          <span class="tip-text">{{$t('km_tip')}}</span>
        </el-col>
      </el-row>
      <el-divider></el-divider>
      <el-row>
        <el-col :span="18">
        <el-form :inline="true" :model="searchdata" class="demo-form-inline" size="small" @submit.native.prevent>
            <el-form-item >
                <el-input v-model="searchdata.data.searchWord"  @keyup.enter.native="searchTableData"  :placeholder="$t('user_name')+'/'+$t('loginno')+'/'+$t('cell_phone')"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="searchTableData" type="primary" plain>{{$t('query')}}</el-button>
                <el-button @click="resetSearchTableData">{{$t('reset')}}</el-button>
            </el-form-item>
            
        </el-form>
        </el-col>
      </el-row>
      <!-- 用户列表 -->
      <el-table :data="tableData.datas"
          @selection-change="onSelectionChange"
          header-cell-class-name="tableHeaderbg" stripe>
          <el-table-column type="selection" width="55" ></el-table-column>
          <el-table-column prop="name" :label="$t('name')"></el-table-column>
          <el-table-column prop="loginno" :label="$t('loginno')"></el-table-column>
          <el-table-column prop="email" :label="$t('email')"></el-table-column>
      </el-table>
      <!-- 分页 -->
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="searchTableData"
            :current-page.sync="searchdata.currpage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="searchdata.pagelines"
            layout="prev, pager, next, total, sizes"
            background
            :total="tableData.rowCount?tableData.rowCount:0"
          ></el-pagination>    
      
    </div>
    </dialog_item> 
</template>

<script>
export default {
  props: ["visible"],
  created(){
    this.searchTableData();
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit("update:visible", val);
      }
    }
  },
  data() {
    return {
      staticPath: this.$api.getStaticPath(),
      searchdata: {
        domainid: this.$route.query.domainid,
        departmentid: "",
        currpage: 1,
        pagelines: 10,
        orderby: "",
        data:{
          searchWord: ""
        }
      },  
      tableData: {},
      multipleSelection: [],
      deptUser: [],
      // kmdata: kmdataOptions,
      checkedKMRoles: [],
    };    
  },
  methods: {
    onBtnDialogClick(name){
      if(name=='confirm'){
        this.onOk()
      }
    },
    resetSearchTableData(){
      if(this.searchdata.data && this.searchdata.data.searchWord)
      this.searchdata.data.searchWord = ''
      this.searchTableData();
    },
    handleSizeChange(size){
      this.searchdata.pagelines=size;
      this.searchdata.currpage=1//页数切换，只需要进行一次请求
      this.searchTableData();
    },
    searchTableData() {
      this.$api.getUsers(this.searchdata, {
        onSucess: resp => {
          this.tableData = resp.data.data;
        }
      });
    },
    onOk(){
      let rowids = [];
      this.multipleSelection.forEach(row => {
        rowids.push(row.id);
      });
      this.ids=rowids.join(",");
      this.kmRoleSelectItem = this.checkedKMRoles.join(",");
      if(this.ids.length==0){
        this.$message({
          showClose: true,
          message: "请选择用户",
          type: "error"
        });
        return false;
      }
      if(this.kmRoleSelectItem.length==0){
        this.$message({
          showClose: true,
          message: "请选择角色",
          type: "error"
        });
        return false;
      }
      let datas = {};
      datas["kmRoleSelectItem"] = this.kmRoleSelectItem;
      datas["ids"] = this.ids;
      this.$api.updatekmconfig(
        {
          domainid: this.$route.query.domainid,
          data: datas
        },  
        {
        onSucess: resp => {
          if (resp.data.errcode == 0) {
            this.$message({
              showClose: true,
              message: "保存成功",
              type: "success"
            });
            this.onCancel();
          } else {
            this.$message({
              showClose: true,
              message: resp.data.errmsg,
              type: "error"
            });
          }
        }
      });
    },
    onCancel() {
      this.$emit("update:visible", false);
    },
    onSelectionChange(rows) {
      this.multipleSelection = rows;
    },
  },
}
</script>